package com.github.catvod.spider.merge;

import com.github.catvod.spider.merge.B;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

/* loaded from: classes.dex */
public class vg extends vt {
    private boolean C4;
    private boolean GM;
    private String[] Ig = {null};
    private ArrayList<Lm> LW;
    private List<String> Og;
    private Ib PD;

    @Nullable
    private Lm PU;
    private B.qM R;
    private boolean b;
    private boolean f;
    private Ib j5;

    @Nullable
    private Lm w4;

    @Nullable
    private mx yx;
    static final String[] oq = {"applet", "caption", "html", "marquee", "object", "table", "td", "th"};
    static final String[] F8 = {"ol", "ul"};
    static final String[] t = {"button"};
    static final String[] n = {"html", "table"};
    static final String[] iU = {"optgroup", "option"};
    static final String[] i4 = {"dd", "dt", "li", "optgroup", "option", "p", "rp", "rt"};
    static final String[] y = {"address", "applet", "area", "article", "aside", "base", "basefont", "bgsound", "blockquote", "body", "br", "button", "caption", "center", "col", "colgroup", "command", "dd", "details", "dir", "div", "dl", "dt", "embed", "fieldset", "figcaption", "figure", "footer", "form", "frame", "frameset", "h1", "h2", "h3", "h4", "h5", "h6", "head", "header", "hgroup", "hr", "html", "iframe", "img", "input", "isindex", "li", "link", "listing", "marquee", "menu", "meta", "nav", "noembed", "noframes", "noscript", "object", "ol", "p", "param", "plaintext", "pre", "script", "section", "select", "style", "summary", "table", "tbody", "td", "textarea", "tfoot", "th", "thead", "title", "tr", "ul", "wbr", "xmp"};

    private boolean F(String[] strArr, String[] strArr2, String[] strArr3) {
        int size = this.I.size() - 1;
        int i = size > 100 ? size - 100 : 0;
        while (size >= i) {
            String w = this.I.get(size).w();
            if (ZG.q(w, strArr)) {
                return true;
            }
            if (ZG.q(w, strArr2)) {
                return false;
            }
            if (strArr3 != null && ZG.q(w, strArr3)) {
                return false;
            }
            size--;
        }
        return false;
    }

    private void HU(fa faVar) {
        mx mxVar;
        if (this.I.isEmpty()) {
            this.q.rl(faVar);
        } else if (r4()) {
            ap(faVar);
        } else {
            h().rl(faVar);
        }
        if (faVar instanceof Lm) {
            Lm lm = (Lm) faVar;
            if (!lm.XA().oC() || (mxVar = this.yx) == null) {
                return;
            }
            mxVar.Kg(lm);
        }
    }

    private boolean N(Lm lm, Lm lm2) {
        return lm.w().equals(lm2.w()) && lm.I().equals(lm2.I());
    }

    private void XA(ArrayList<Lm> arrayList, Lm lm, Lm lm2) {
        int lastIndexOf = arrayList.lastIndexOf(lm);
        R2.j(lastIndexOf != -1);
        arrayList.set(lastIndexOf, lm2);
    }

    private void n(String... strArr) {
        for (int size = this.I.size() - 1; size >= 0; size--) {
            Lm lm = this.I.get(size);
            if (ZG.j(lm.w(), strArr) || lm.w().equals("html")) {
                return;
            }
            this.I.remove(size);
        }
    }

    private boolean s(ArrayList<Lm> arrayList, Lm lm) {
        int size = arrayList.size() - 1;
        int i = size >= 256 ? size - 256 : 0;
        while (size >= i) {
            if (arrayList.get(size) == lm) {
                return true;
            }
            size--;
        }
        return false;
    }

    private boolean z(String str, String[] strArr, String[] strArr2) {
        String[] strArr3 = this.Ig;
        strArr3[0] = str;
        return F(strArr3, strArr, strArr2);
    }

    void B(Lm lm, int i) {
        F8(lm);
        this.LW.add(i, lm);
    }

    boolean C(Lm lm) {
        return ZG.q(lm.w(), y);
    }

    Lm C4() {
        return this.PU;
    }

    Lm DQ() {
        int size = this.LW.size();
        if (size > 0) {
            return this.LW.remove(size - 1);
        }
        return null;
    }

    Lm E(B.x xVar) {
        iP iU2 = iP.iU(xVar.C4(), this.W);
        Lm lm = new Lm(iU2, null, this.W.V0(xVar.Qy));
        HU(lm);
        if (xVar.b()) {
            if (!iU2.W()) {
                iU2.t();
            } else if (!iU2.I()) {
                this.j.j5("Tag cannot be self closing; not a void tag");
            }
        }
        return lm;
    }

    void F8(Lm lm) {
        int i = 0;
        for (int size = this.LW.size() - 1; size >= 0; size--) {
            Lm lm2 = this.LW.get(size);
            if (lm2 == null) {
                return;
            }
            if (N(lm, lm2)) {
                i++;
            }
            if (i == 3) {
                this.LW.remove(size);
                return;
            }
        }
    }

    Lm FE(B.x xVar) {
        if (xVar.R() && !xVar.Qy.isEmpty() && xVar.Qy.Qy(this.W) > 0) {
            j("Duplicate attribute");
        }
        if (!xVar.b()) {
            Lm lm = new Lm(iP.iU(xVar.C4(), this.W), null, this.W.V0(xVar.Qy));
            hr(lm);
            return lm;
        }
        Lm E = E(xVar);
        this.I.add(E);
        this.j.w4(xb.h);
        this.j.t(this.R.t().f(E.MC()));
        return E;
    }

    boolean GM() {
        return this.b;
    }

    void Gg(Lm lm) {
        for (int size = this.LW.size() - 1; size >= 0; size--) {
            if (this.LW.get(size) == lm) {
                this.LW.remove(size);
                return;
            }
        }
    }

    boolean H9(String str) {
        return z(str, n, null);
    }

    ArrayList<Lm> Ig() {
        return this.I;
    }

    void J(mx mxVar) {
        this.yx = mxVar;
    }

    void K(Lm lm, Lm lm2) {
        XA(this.LW, lm, lm2);
    }

    void Kg(boolean z) {
        this.C4 = z;
    }

    void L0(Ib ib) {
        this.PD = ib;
    }

    String LW() {
        return this.oC;
    }

    void M() {
        this.LW.add(null);
    }

    void MC(Lm lm, Lm lm2) {
        XA(this.I, lm, lm2);
    }

    Lm O() {
        if (this.LW.size() <= 0) {
            return null;
        }
        return this.LW.get(r0.size() - 1);
    }

    HZ Og() {
        return this.q;
    }

    int Oh(Lm lm) {
        for (int i = 0; i < this.LW.size(); i++) {
            if (lm == this.LW.get(i)) {
                return i;
            }
        }
        return -1;
    }

    void P() {
        Lm O = O();
        if (O == null || a(O)) {
            return;
        }
        boolean z = true;
        int size = this.LW.size() - 1;
        int i = size;
        while (i != 0) {
            i--;
            O = this.LW.get(i);
            if (O == null || a(O)) {
                z = false;
                break;
            }
        }
        while (true) {
            if (!z) {
                i++;
                O = this.LW.get(i);
            }
            R2.u(O);
            Lm d1 = d1(O.w());
            d1.I().I(O.I());
            this.LW.set(i, d1);
            if (i == size) {
                return;
            } else {
                z = false;
            }
        }
    }

    void PD(Ib ib) {
        if (this.h.h().h()) {
            this.h.h().add(new lA(this.V0.V(), "Unexpected token [%s] when in state [%s]", this.BQ.iU(), ib));
        }
    }

    void PU() {
        yx(null);
    }

    boolean Pi(Lm lm) {
        for (int size = this.I.size() - 1; size >= 0; size--) {
            if (this.I.get(size) == lm) {
                this.I.remove(size);
                return true;
            }
        }
        return false;
    }

    mx R() {
        return this.yx;
    }

    Lm S(String str) {
        for (int size = this.I.size() - 1; size >= 0; size--) {
            Lm lm = this.I.get(size);
            this.I.remove(size);
            if (lm.w().equals(str)) {
                return lm;
            }
        }
        return null;
    }

    void SS(Lm lm, Lm lm2) {
        int lastIndexOf = this.I.lastIndexOf(lm);
        R2.j(lastIndexOf != -1);
        this.I.add(lastIndexOf + 1, lm2);
    }

    boolean T(String str, String[] strArr) {
        return z(str, oq, strArr);
    }

    void TD(Lm lm) {
        this.PU = lm;
    }

    boolean To(String str) {
        for (int size = this.I.size() - 1; size >= 0; size--) {
            String w = this.I.get(size).w();
            if (w.equals(str)) {
                return true;
            }
            if (!ZG.q(w, iU)) {
                return false;
            }
        }
        R2.h("Should not be reachable");
        return false;
    }

    Ib U() {
        return this.PD;
    }

    boolean V(String[] strArr) {
        return F(strArr, oq, null);
    }

    @Override // com.github.catvod.spider.merge.vt
    Qg V0() {
        return Qg.h;
    }

    Ib Xu() {
        return this.j5;
    }

    void Y(String str) {
        for (int size = this.I.size() - 1; size >= 0 && !this.I.get(size).w().equals(str); size--) {
            this.I.remove(size);
        }
    }

    boolean Yd(B b, Ib ib) {
        this.BQ = b;
        return ib.Qy(b, this);
    }

    boolean a(Lm lm) {
        return s(this.I, lm);
    }

    void ap(fa faVar) {
        Lm lm;
        Lm b = b("table");
        boolean z = false;
        if (b == null) {
            lm = this.I.get(0);
        } else if (b.C4() != null) {
            lm = b.C4();
            z = true;
        } else {
            lm = oq(b);
        }
        if (!z) {
            lm.rl(faVar);
        } else {
            R2.u(b);
            b.SS(faVar);
        }
    }

    Lm b(String str) {
        for (int size = this.I.size() - 1; size >= 0; size--) {
            Lm lm = this.I.get(size);
            if (lm.w().equals(str)) {
                return lm;
            }
        }
        return null;
    }

    Lm d1(String str) {
        Lm lm = new Lm(iP.iU(str, this.W), null);
        hr(lm);
        return lm;
    }

    void e() {
        boolean z = false;
        for (int size = this.I.size() - 1; size >= 0; size--) {
            Lm lm = this.I.get(size);
            if (size == 0) {
                lm = this.w4;
                z = true;
            }
            String w = lm != null ? lm.w() : "";
            if ("select".equals(w)) {
                L0(Ib.i4);
                return;
            }
            if ("td".equals(w) || ("th".equals(w) && !z)) {
                L0(Ib.iU);
                return;
            }
            if ("tr".equals(w)) {
                L0(Ib.n);
                return;
            }
            if ("tbody".equals(w) || "thead".equals(w) || "tfoot".equals(w)) {
                L0(Ib.t);
                return;
            }
            if ("caption".equals(w)) {
                L0(Ib.oq);
                return;
            }
            if ("colgroup".equals(w)) {
                L0(Ib.F8);
                return;
            }
            if ("table".equals(w)) {
                L0(Ib.u);
                return;
            }
            if ("head".equals(w)) {
                L0(Ib.BQ);
                return;
            }
            if ("body".equals(w)) {
                L0(Ib.BQ);
                return;
            }
            if ("frameset".equals(w)) {
                L0(Ib.j5);
                return;
            } else if ("html".equals(w)) {
                L0(Ib.j);
                return;
            } else {
                if (z) {
                    L0(Ib.BQ);
                    return;
                }
            }
        }
    }

    List<String> f() {
        return this.Og;
    }

    boolean fR(Lm lm) {
        return s(this.LW, lm);
    }

    boolean g6() {
        return this.f;
    }

    void hP() {
        this.j5 = this.PD;
    }

    void hr(Lm lm) {
        HU(lm);
        this.I.add(lm);
    }

    protected boolean i(String str) {
        return str.equals("script") || str.equals("style");
    }

    void i4() {
        n("table");
    }

    void iU() {
        n("tbody", "tfoot", "thead", "template");
    }

    void j5(boolean z) {
        this.b = z;
    }

    boolean l(String str) {
        return T(str, t);
    }

    void lu(Lm lm) {
        this.I.add(lm);
    }

    void nO(Lm lm) {
        if (this.GM) {
            return;
        }
        String h = lm.h("href");
        if (h.length() != 0) {
            this.oC = h;
            this.GM = true;
            this.q.F(h);
        }
    }

    boolean nf(String str) {
        return T(str, F8);
    }

    @Override // com.github.catvod.spider.merge.vt
    protected boolean oC(B b) {
        this.BQ = b;
        return this.PD.Qy(b, this);
    }

    Lm oq(Lm lm) {
        for (int size = this.I.size() - 1; size >= 0; size--) {
            if (this.I.get(size) == lm) {
                return this.I.get(size - 1);
            }
        }
        return null;
    }

    @Override // com.github.catvod.spider.merge.vt
    @ParametersAreNonnullByDefault
    protected void q(Reader reader, String str, F f) {
        super.q(reader, str, f);
        this.PD = Ib.h;
        this.j5 = null;
        this.GM = false;
        this.PU = null;
        this.yx = null;
        this.w4 = null;
        this.LW = new ArrayList<>();
        this.Og = new ArrayList();
        this.R = new B.qM();
        this.b = true;
        this.C4 = false;
        this.f = false;
    }

    void qG(B.d dVar) {
        HU(new mF(dVar.j5()));
    }

    boolean r4() {
        return this.C4;
    }

    Lm rG() {
        return this.I.remove(this.I.size() - 1);
    }

    boolean rd(String str) {
        return T(str, null);
    }

    mx rl(B.x xVar, boolean z) {
        mx mxVar = new mx(iP.iU(xVar.C4(), this.W), null, this.W.V0(xVar.Qy));
        J(mxVar);
        HU(mxVar);
        if (z) {
            this.I.add(mxVar);
        }
        return mxVar;
    }

    void t() {
        while (!this.LW.isEmpty() && DQ() != null) {
        }
    }

    public String toString() {
        return "TreeBuilder{currentToken=" + this.BQ + ", state=" + this.PD + ", currentElement=" + h() + '}';
    }

    @Override // com.github.catvod.spider.merge.vt
    public /* bridge */ /* synthetic */ boolean u(String str, QF qf) {
        return super.u(str, qf);
    }

    void v(B.HA ha) {
        Lm h = h();
        if (h == null) {
            h = this.q;
        }
        String w = h.w();
        String y2 = ha.y();
        h.rl(ha.oC() ? new ok(y2) : i(w) ? new BG(y2) : new IG(y2));
    }

    void vm(Lm lm) {
        F8(lm);
        this.LW.add(lm);
    }

    void w(String... strArr) {
        for (int size = this.I.size() - 1; size >= 0; size--) {
            Lm lm = this.I.get(size);
            this.I.remove(size);
            if (ZG.q(lm.w(), strArr)) {
                return;
            }
        }
    }

    Lm w4(String str) {
        for (int size = this.LW.size() - 1; size >= 0; size--) {
            Lm lm = this.LW.get(size);
            if (lm == null) {
                return null;
            }
            if (lm.w().equals(str)) {
                return lm;
            }
        }
        return null;
    }

    void y() {
        n("tr", "template");
    }

    void yp() {
        this.Og = new ArrayList();
    }

    void yx(String str) {
        while (str != null && !h().w().equals(str) && ZG.q(h().w(), i4)) {
            rG();
        }
    }
}
